package at.sysarch.filter;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashSet;
import java.util.LinkedList;

import at.pimpmypipe.filter.DataEnrichmentFilter;
import at.sysarch.model.Line;

public class ShiftListFilter extends DataEnrichmentFilter<LinkedList<Line>, LinkedList<Line>> {
	
	private final HashSet<String> _uselessWords;

	/**
	 * Reads entries from 1000words.txt into the HashSet _uselessWords.
	 */
	public ShiftListFilter() {
		
		_uselessWords = new HashSet<String>();
		
		BufferedReader reader = null;
		try {
			reader = new BufferedReader(new FileReader("./Files/1000words.txt"));
			String word = null;
			do {
				
				word = reader.readLine();
				
				if (word != null) {
					
					_uselessWords.add(word);
				}
				
			} while (word != null);
			
		} catch (IOException e) {

			e.printStackTrace();
		} finally {
			if (reader != null) {
				
				try {
					reader.close();
				} catch (IOException e) {

					e.printStackTrace();
				}
			}
		}
	}
	
	/**
	 * Takes a list of words and shifts it to the left.
	 * Result is a List of lines with index, each shifted one word to the left.
	 * Shifts beginning with a word contained in 1000Words.txt are not included.
	 */
	@Override
	protected boolean fillEntity(LinkedList<Line> nextVal, LinkedList<Line> entity) {

		int listLength = nextVal.size();
		
		if (listLength == 0) {
			return false;
		}
		
		int index = nextVal.getFirst().getIndex();
		
		for (int i = 0; i < listLength; i++) {

			if (!_uselessWords.contains(nextVal.getFirst().getData())) {
				StringBuilder builder = new StringBuilder();
				
				for (int j = 0; j < listLength; j++) {
					
					builder.append(nextVal.get(j).getData());
					if (j < listLength - 1) {
						builder.append(" ");
					}
				}
								
				Line line = new Line();
				line.setData(builder.toString());
				line.setIndex(index);
				
				entity.add(line);
			}
			nextVal.addLast(nextVal.removeFirst());
		}
		
		return true;
	}

	@Override
	protected LinkedList<Line> getNewEntityObject() {

		return new LinkedList<Line>();
	}

}
